package com.msl.debezium.conf;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.google.common.collect.Lists;
import com.msl.debezium.common.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.net.InetAddress;
import java.util.List;

@Configuration
@EnableSwagger2
@EnableKnife4j
public class Swagger2Conf {

    private final Logger logger = LoggerFactory.getLogger(Swagger2Conf.class);

    @Autowired
    private Docket docket;

    @Autowired
    private DocumentationPluginsBootstrapper documentationPluginsBootstrapper;

    @Autowired
    private RedisUtil redis;

    @Value("${spring.datasource.vm.jdbc-url}")
    private String vmUrl;

    @Value("${spring.datasource.win.jdbc-url}")
    private String winUrl;

    //@Value("${swagger.host}")
    private String swaggerHost;

    @Bean
    public Docket docket(){
        ParameterBuilder builder = new ParameterBuilder();
        Parameter parameter = builder
                // 从cookie中获取token
                .parameterType("cookie") //参数类型支持header, cookie, body, query etc
                .name("token") //参数名
                .defaultValue("") //默认值
                .description("请输入token")
                .modelRef(new ModelRef("string")) //指定参数值的类型
                .required(false).build(); //非必需，这里是全局配置，然而在登陆的时候是不用验证的
        List<Parameter> parameters = Lists.newArrayList(parameter);
        return new Docket(DocumentationType.SWAGGER_2)
                //.host(this.swaggerHost)
                .apiInfo(ApiInfo.DEFAULT)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.msl.debezium"))
                .paths(PathSelectors.any())
                .build();
                //.globalOperationParameters(parameters);
    }

    public ApiInfo apiInfo(String podName, String dbDatasource, String url) {
        return new ApiInfoBuilder()
                .title("Debezium Service")
                .description(dbDatasource + "@" + podName)
                .termsOfServiceUrl(url)
                .version("1.0.0")
                .contact(new Contact("Yaguang Kang", "www.baidu.com", "710@qq.com"))
                .build();
    }

    public void changeDocket(String podName, String dbDatasource){
        String url = "AKS".equals(dbDatasource)?vmUrl:winUrl;
        docket.apiInfo(apiInfo(podName, dbDatasource, url));
        documentationPluginsBootstrapper.stop();
        documentationPluginsBootstrapper.start();
    }

    public String getLocalName(){
        String local = "";
        try {
            local = InetAddress.getLocalHost().getHostName();
            logger.info("local为：" + local);
        } catch (Exception e) {
            logger.error("未获取到地址信息");
        }
        return local;
    }
}
